<template>
  <el-row :gutter="20">
    <el-col :span="16"
      ><div class="grid-content bg-purple">
        <div class="up">
          <van-uploader :after-read="afterRead"> </van-uploader>
          <i class="el-icon-folder-add"></i>
          <p :style="{ fontSize: '20px' }">点击上传轮播图</p>
        </div>

        <div class="imgs">
          <p>轮播图详情</p>
          <ul>
            <li v-for="item in bannerdata" :key="item.bannerid">
              <img :src="`${url}${item.img}`" alt="" />
              <el-button type="info" @click="setid(item.bannerid)"
                >删除轮播图</el-button
              >
            </li>
          </ul>
        </div>
      </div></el-col
    >
    <el-col :span="8"
      ><div class="grid-content bg-purple right">
        <p>轮播图预览</p>
        <div class="phone" v-if="bannerdata">
          <div class="banner">
            <div class="block">
              <el-carousel height="150px">
                <el-carousel-item
                  v-for="(item, index) in bannerdata"
                  :key="index"
                >
                  <img :src="`${url}${item.img}`" alt="" />
                </el-carousel-item>
              </el-carousel>
            </div>
          </div>
          <div class="main" :style="{ marginTop: '10vh' }">
            <el-skeleton :rows="8" />
          </div>
        </div></div
    ></el-col>

    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <span>确定删除吗</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="remove">确 定</el-button>
      </span>
    </el-dialog>
  </el-row>
</template>

<script>
export default {
  name: "MyBanner",
  data() {
    return {
      bannerdata: null,
      dialogVisible: false,
      bannerid: "",
      url: "http://192.168.35.36:3005",
    };
  },
  methods: {
    setid(id) {
      this.dialogVisible = true;
      this.bannerid = id;
      console.log(this.bannerid);
    },
    handleClose(done) {
      this.$confirm("确认关闭？").then(() => {
        this.bannerid = "";
        done();
      });
    },
    remove() {
      this.dialogVisible = false;
      console.log(this.bannerdata);
      if (this.bannerdata.length <= 2) {
        this.$message("至少保留两张");
        return;
      } else {
        this.$axios
          .post(`${this.url}/home/remove`, {
            id: this.bannerid,
          })
          .then(() => {
            this.getdata();
          });
      }
    },
    afterRead(file) {
      this.$axios
        .post(
          `${this.url}/upload/geturl`,
          {
            photo: file.file,
            fileType: "IMAGE",
          },
          { headers: { "Content-Type": "multipart/form-data" } }
        )
        .then((content) => {
          this.$axios
            .post(`${this.url}/home/addbanner`, {
              url: content.data.data,
              type: 1,
            })
            .then((text) => {
              this.getdata();
              console.log(text);
            });
        });
    },
    getdata() {
      this.$axios.get(`${this.url}/home/man`).then((content) => {
        this.bannerdata = content.data.banner;
      });
    },
  },
  created() {
    this.getdata();
  },
};
</script>

<style lang="less" scoped>
.imgs {
  p {
    font-size: 18px;
    line-height: 40px;
  }
  ul {
    height: 70vh;
    overflow: scroll;
    margin-top: 5vh;
    li {
      display: flex;
      justify-content: space-between;
      margin-bottom: 3vh;
      align-items: center;
      padding: 0 2vw;
      .el-button {
        height: 50px;
      }
      img {
        width: 80%;
        height: 360px;
      }
    }
  }
}
.up {
  position: relative;
  height: 100px;
  .van-uploader {
    margin-top: 20px;
    height: 100px;
    position: absolute;
    left: 20px;
    z-index: 2;
    opacity: 0;
    margin-left: 5vw;
  }
  i {
    position: absolute;
    line-height: 100px;
    width: 140px;
    background-color: white;
    font-size: 40px;
    left: 50px;
    opacity: 1;
  }
  p {
    position: absolute;
    left: 20%;
  }
}

.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
.phone {
  width: 375px;
  height: 80vh;
  background-color: white;
  margin: 0 auto;
  border-radius: 10px;
  overflow: hidden;

  img {
    width: 100%;
    height: 150px;
  }
}
.right {
  padding: 3vh 0;
  p {
    font-size: 18px;
    font-weight: 1000;
    line-height: 40px;
  }
}
</style>